<template>
  <div>
    <div>
      <el-button type="success" size='small' @click="dialogVisible()" class="addClick">{{$t('添加')}}</el-button>
      <el-button type="danger" size='small' @click="deleteData" class="delClick">{{$t('删除')}}</el-button>
      <el-button type="danger" size='small' @click="clearData" class="clearClick">{{$t('清空')}}</el-button>
      <!-- <span style="margin-left:10px;" class="changeUnmatchModeSwitch">{{$t('拦截日志')}}：
        <el-switch :width="40" v-model="InterceptLog" class="InterceptLog" @change="changeInterceptLog" active-color="#009966" inactive-color="#CCCCCC" active-value="on" inactive-value="off" :class="{  'switch-is-actived': InterceptLog === 'on'  }"></el-switch>
      </span> -->
    </div>
    <el-table size='mini' ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" align='center' width="55">
      </el-table-column>
      <el-table-column :label="$t('源IP')" align='center' min-width="100">
        <template slot-scope="scope">{{ scope.row.src }}</template>
      </el-table-column>
      <el-table-column :label="$t('目的IP')" align='center' min-width="100">
        <template slot-scope="scope">{{ scope.row.des }}</template>
      </el-table-column>
      <el-table-column :label="$t('源端口')" align='center' min-width="100">
        <template slot-scope="scope">{{ scope.row.sport }}</template>
      </el-table-column>
      <el-table-column :label="$t('目的端口')" align='center' min-width="100">
        <template slot-scope="scope">{{ scope.row.dport }}</template>
      </el-table-column>
      <el-table-column :label="$t('源MAC')" align='center' min-width="100">
        <template slot-scope="scope">{{ scope.row.smac }}</template>
      </el-table-column>
      <el-table-column :label="$t('协议')" align='center' min-width="100">
        <template slot-scope="scope">{{ scope.row.prot }}</template>
      </el-table-column>
      <el-table-column :label="$t('IPV4范围')" align='center' min-width="100">
        <template slot-scope="scope">{{ scope.row.domain }}</template>
      </el-table-column>
    </el-table>
    <StaticBlacklistDialog v-if="staticBlacklistDialogVisible" ref="refStaticBlacklistDialogVisible"></StaticBlacklistDialog>
  </div>
</template>
<script>
import { getStaticBlacklist, delStaticBlack, clearStaticBlack } from '@/api/safe/blacklist'
import StaticBlacklistDialog from './components/StaticBlacklistDialog'
import { getInterceptLogSwitch, setInterceptLogSwitch } from '@/api/safe/black'
export default {
  name: 'StaticBlacklist',
  components: {
    StaticBlacklistDialog
  },
  data () {
    return {
      staticBlacklistDialogVisible: false,
      InterceptLog: 'on',
      tableData: [],
      addrStr: ''
    }
  },
  created () {
    this.listData()
    // getInterceptLogSwitch().then(res => {
    //   if (res.status === 0 && res.info[0]) {
    //     this.InterceptLog = res.info[0].switch
    //   }
    // })
  },
  methods: {
    // 多选按钮
    handleSelectionChange (data) {
      var addrStr = ''
      for (var item of data) {
        if (addrStr === '') {
          addrStr = item.id
        } else {
          addrStr = addrStr + '@#@' + item.id
        }
      }
      this.addrStr = addrStr
    },
    // 列表
    listData () {
      getStaticBlacklist().then(res => {
        if (res.status === 0) {
          this.tableData = res.info
        }
      })
    },
    // 添加按钮表单
    dialogVisible (data) {
      this.staticBlacklistDialogVisible = true
      this.$nextTick(() => {
        this.$refs.refStaticBlacklistDialogVisible.init(data)
      })
    },
    deleteData () {
      if (this.addrStr !== '') {
        this.$confirm(this.$t('您是否要删除所有选中的数据？'), this.$t('提示'), {
          closeOnClickModal: false,
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          confirmButtonClass: 'confirmButton',
          cancelButtonClass: 'confirmCancelButton',
          type: 'warning'
        }).then(() => {
          delStaticBlack({ id: this.addrStr }).then(res => {
            if (res.status === 0) {
              this.$messages.success(this.$t('删除成功'))
              this.addrStr = ''
              this.listData()
            }
          })
        })
      } else {
        this.$messages.error(this.$t('请至少选择一条数据进行删除'))
      }
    },
    clearData () {
      this.$confirm(this.$t('您是否要清空所有数据？'), this.$t('提示'), {
        closeOnClickModal: false,
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        confirmButtonClass: 'confirmButton',
        cancelButtonClass: 'confirmCancelButton',
        type: 'warning'
      }).then(() => {
        clearStaticBlack().then(res => {
          if (res.status === 0) {
            this.$messages.success(this.$t('清空成功'))
            this.listData()
          }
        })
      })
    },
    changeInterceptLog (data) {
      setInterceptLogSwitch({ switch: data }).then(res => {
        if (res.status === 0) {
          this.$messages.success(res.msg)
        }
      }).catch(() => {
        this.InterceptLog = this.InterceptLog === 'on' ? 'off' : 'on'
        this.$messages.info('拦截日志' + (this.InterceptLog === 'on' ? '开' : '关'))
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.changeUnmatchModeSwitch {
  .el-switch {
    /deep/ .el-switch__core {
      &::before {
        content: "关";
        color: #fff;
        position: absolute;
        // font-size: 14px;
        transform: scale(0.85);
        left: 20px;
      }
    }

    &.switch-is-actived {
      /deep/ .el-switch__core {
        &::before {
          content: "开";
          left: 6px;
          transform: scale(0.85);
        }
      }
    }
  }
}
</style>
